package leetcode;

public class problems_014 {
    public static void main(String[] arge){
        String[] xxx = {"aa","a"};
        System.out.println(new Solution().longestCommonPrefix(xxx));
    }

    static class Solution {
        public String longestCommonPrefix(String[] strs) {
            if (null == strs || strs.length == 0 || null == strs[0] || "".equals(strs)) return "";
            StringBuffer ret = new StringBuffer();
            for (int i = 0; i < strs[0].length(); i++) {
                boolean isEquals = true;
                for (int j = 1; j < strs.length; j++) {
                    if(strs[j].length() < (i+1) || strs[j].charAt(i) != strs[0].charAt(i)){
                        isEquals = false;
                    }
                }
                if(isEquals){
                    ret.append(strs[0].charAt(i));
                }else {
                    break;
                }
            }
            return ret.toString();
        }
    }
}
